<template>
  <div class="news-classify">
    <div style="display:flex;align-items: center;justify-content: space-between;margin:0 0 10px;">
      <div style="display:flex;align-items: center;">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="话题分类">
            <el-select clearable size="small" v-model="formInline.tagCategoryId" placeholder="请选择话题">
              <el-option
                v-for="item in oneTagList"
                :key="item.code"
                :label="item.value"
                :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select clearable size="small" v-model="formInline.state" placeholder="请选择话题">
              <el-option
                v-for="item in [{
                  value: '1',
                  label: '有效'
                },
                {
                  value: '0',
                  label: '无效'
                }]"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <el-button size="small" type="primary" @click="onSubmit">搜索</el-button>
        <el-button size="small" @click="resetSubmit">重置</el-button>
      </div>
      <div>
        <el-button size="small" type="primary" @click="add">新建话题</el-button>
      </div>
    </div>
    <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
      <el-table-column label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="缩略图">
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" style="width:100px;maxHeight:80px;" alt="">
        </template>
      </el-table-column>
      <el-table-column label="话题名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="话题分类" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ scope.row.tagCategoryName }}
        </template>
      </el-table-column>
      <el-table-column label="排序">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column label="话题介绍" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ scope.row.detail }}
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 0" size="small" type='danger'>无效</el-tag>
          <el-tag v-else size="small" type='success'>有效</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="changeState(scope.row, 0)" type="text" style="color:#ff0000;" v-if="scope.row.state == 1">设置无效</el-button>
          <el-button @click="changeState(scope.row, 1)" type="text" v-else>设置有效</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button @click="edit(scope.row)" type="text">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            @onConfirm="del(scope.row)"
            confirm-button-text='确定'
            cancel-button-text='取消'
            icon="el-icon-info"
            icon-color="red"
            title="是否删除本条数据？">
            <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin:10px 0 0 0;text-align:right;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 30, 50, 100]"
        :page-size="1"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageObj.total">
      </el-pagination>
    </div>

    <el-dialog :title="dailogTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="话题名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getThreadTagApi,
  threadTagChangeStateApi,
  threadTagBatchDeleteApi,
  getOneTypeListApi,
} from '@/api/forum'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      dialogFormVisible: false,
      dailogTitle: '新建分类',
      formInline: {
        tagCategoryId: '',
        state: '',
      },
      form: {
        name: '',
        region: '',
      },
      formLabelWidth: '80px',
      currentPage: 1,
      pageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      oneTagList: []
    }
  },
  created() {
    this.fetchData()
    this.getOneType()
  },
  methods: {
    getOneType() {
      getOneTypeListApi({
        type: 'video'
      }).then((res) => {
        this.oneTagList = res.data
      })
    },
    onSubmit() {
      this.fetchData()
    },
    resetSubmit() {
      this.pageObj.pageSize = 10
      this.pageObj.pageNum = 1
      this.formInline.tagCategoryId = ''
      this.formInline.state = ''
      this.fetchData()
    },
    del(row) {
      threadTagBatchDeleteApi({
        ids: row.id
      }).then((res) => {
        this.fetchData()
      })
    },
    changeState(row, state) {
      threadTagChangeStateApi({
        id: row.id,
        state: state
      }).then((res) => {
        this.fetchData()
      })
    },
    add() {
      this.$router.push({
        path: '/video/videoAdd'
      })
    },
    edit(row) {
      this.$router.push({
        path: '/video/videoEdit',
        query: {
          editId: row.id
        }
      })
    },
    fetchData() {
      this.listLoading = true
      getThreadTagApi({
        pageSize: this.pageObj.pageSize,
        pageNum: this.pageObj.pageNum,
        state: this.formInline.state,
        tagCategoryId: this.formInline.tagCategoryId,
        type: 'video',
      }).then(response => {
        this.listLoading = false
        this.list = response.data.result
        this.pageObj.total = response.data.total
      })
    },
    handleSizeChange(val) {
      this.pageObj.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageObj.pageNum = val
      this.fetchData()
    }
  }
}
</script>

<style lang="scss" scoped>
   .news-classify{
     padding: 20px;
     .el-form-item{
       margin-bottom: 0;
     }
   }
</style>
